package com.cy.module;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cy.BasicController;
import com.cy.model.Account;
import com.cy.model.Menu;
import com.cy.utils.PropertyUtil;
import com.cy.utils.dingtalk.DingTalkUtils;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.JsonKit;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class HomeController extends BasicController {

    @ActionKey("/")
    public void index() {
        setAttr("redirectUri", PropertyUtil.getGrosseValue(PropertyUtil.KERNEL, "dingTalkQrCodeLoginCallback"));
        setAttr("qrCodeAuthorizeUrl", DingTalkUtils.QR_CODE_AUTHORIZE_IF);
        setAttr("appid", DingTalkUtils.APP_ID);
        renderJsp("/view/login.jsp");
    }

    @ActionKey("/clmp/main")
    public void main(){
        Account account = getLoginAccount();
        List<Menu> menus = account.getPlatfromShowMenus();
        //分组
        Map<Menu, List<Menu>> groupBy = new LinkedHashMap<>();
        Map<Long, Menu> topMenus = new HashMap<>();
        for (Menu menu : menus){
            if (menu.getParentId() == null){
                //顶级菜单
                topMenus.put(menu.getId(), menu);
                groupBy.put(menu, new ArrayList<Menu>());
            }
        }
        for (Menu menu : menus){
            //二级菜单
            if (menu.getParentId() != null){
                Menu tm = topMenus.get(menu.getParentId());
                if (tm != null)
                    groupBy.get(tm).add(menu);
            }
        }

        JSONArray menuJsonArray = new JSONArray(menus.size());
        String contextPath = getRequest().getContextPath();
        //转换JSON
        for (Map.Entry<Menu, List<Menu>> tm : groupBy.entrySet()){
            JSONObject menuJsonObject = new JSONObject(2);
            menuJsonObject.put("text", tm.getKey().getName());
            List<Menu> children = tm.getValue();
            if (children.size() > 0){
                JSONArray childMenuJsonArray = new JSONArray(children.size());
                for (Menu child : children){
                    JSONObject childMenuJsonObject = new JSONObject(3);
                    String url = child.getUrl();
                    String id = null;
                    if (StringUtils.isNotBlank(url)){
                        int bi = url.lastIndexOf("/");
                        int ei = url.lastIndexOf(".");
                        id = StringUtils.uncapitalize(url.substring(bi + 1, ei));
                    }
                    childMenuJsonObject.put("id", id);
                    childMenuJsonObject.put("text", child.getName());
                    childMenuJsonObject.put("href", url == null ? null : contextPath.concat(url));
                    childMenuJsonArray.add(childMenuJsonObject);
                }
                menuJsonObject.put("items", childMenuJsonArray);
            }
            menuJsonArray.add(menuJsonObject);
        }
        account.remove("salt", "password");
        setAttr("menus", menuJsonArray);
        setAttr("loginedUser", JsonKit.toJson(account));
        renderJsp("/view/main.jsp");
    }

}
